/**
	@author yangcaho message         
**/
YOUXIXIU = window.YOUXIXIU || {};
YOUXIXIU.MAIN= YOUXIXIU.MAIN || {};
YOUXIXIU.MAIN.AlbumAddContent = (function($) {
	var html='';
	var status={notice:false,atme:false,unread:false}
	var notice_num,atme_num,unread_num;
	var opt={};
	var defaults={
		add_album_content_button:'.add_album_content',
		tpl_id:'poup_add_mblog',
		close_poup_class:"close_poup",
		add_time:0,
		album_id:0,
		reload:0,	
		title:"分享游戏秀"

	};
	//-----global functions ------// 
	function chkDefault(options)
	{
		for(key in defaults)
		{
			if(typeof(options[key])=='undefined')
			{
				options[key]=defaults[key];
			}
		}
		return options;
	}

	
	function bindclose()
	{
		$("."+opt.close_poup_class).die("click");
		$("."+opt.close_poup_class).live("click",function(e){

			close_poup();
		});
		
		
	}

	function close_poup()
	{
		$("."+opt.contain_class).remove();
		$(".sm-mask").hide();//蒙层
	}
	

	function show_select()
	{	
			$(opt.add_album_content_button).bind("click",function(e){
		
			
				$CONFIG= opt;
				$(".light_box_fullbg").show();//蒙层
				$("#album_add_content_select").tmpl($CONFIG).appendTo($("body"));

			
				//绑定图片上传事件
				options={
				callback:'/js/uploader_callback.html',
				container:'.pic_input_file_div',
				script:'/interface/common/json_mblog_pic_upload.php',
				size:"300|300",
				thumb:1,
				onComplete:function(res){
				
					$(".album_add_content_div").remove();
					var info ={};
					info.pic = res.pic2.replace("/thumbnail/","/thumb150/"); ;
					$("#album_add_content_local").tmpl(info).appendTo($("body"));
					bind_album_add_content_local(info.pic);


				},
					errorId:'#errormsg'
				};
				YOUXIXIU.ImgUploader.init(options);


				//远程图片点击
				$(".chose_goods").bind("click",function(e){
					$(".album_add_content_div").remove();
					var info ={};
					$("#album_add_content_online").tmpl(info).appendTo($("body"));
					bind_album_add_content_online();
				});


				//关闭事件
				$(".album_add_content_div").find(".lb_close").die("click");
				$(".album_add_content_div").find(".lb_close").live("click",function(e){
					$(".album_add_content_div").remove();
					$(".light_box_fullbg").hide();//蒙层
				});
				
				//选择类型事件
				$(".album_add_content_div").find(".sort").find(".cur").live("click",function(e){
					
					$(this).find("ul").toggle();
				});

				$(".album_add_content_div").find(".sort").find("li").die("click");
				$(".album_add_content_div").find(".sort").find("li").live("click",function(e){
					var get_val = $(this).attr("value");
					var get_name = $(this).find("a").html();
					$(this).parents(".sort").find(".select_name").html(get_name);
					$(this).parents(".sort").find("#add_mblog_topic_id").val(get_val);
					
				});

				

			});
			
			
		


	}

	function bind_album_add_content_local(pic)
	{

		//绑定默认选中的类型（如果是加入专辑）
		if(opt.add_mblog_default_topic_id)
		{
			var default_topic = $(".album_add_content_div").find(".sort").find("li[value="+opt.add_mblog_default_topic_id+"]").find("a").html();
			$(".album_add_content_div").find(".sort").find(".select_name").html(default_topic);
			$(".album_add_content_div").find(".sort").find("#add_mblog_topic_id").val(opt.add_mblog_default_topic_id);
		}
		//绑定提示内容默认值
		YOUXIXIU.MAIN.Tips.text_default_val(".add_mblog_local_default_content"); 
		

		$(".bind_album_add_content_local_button").unbind("click");
		$(".bind_album_add_content_local_button").bind("click",function(e){
			content = $("#pub_content_img").val();
			topic_id = $(".album_add_content_div").find("#add_mblog_topic_id").val();
			$.getJSON("/interface/album/json_add_pic_url_to_album.php?t=jsonp&album_id="+opt.album_id+"&pic="+pic+"&content="+content+"&topic_id="+topic_id+"&&callback=?", function(json){
			if(typeof(json)!='undefined')
			{
				if(json.error ==1)
				{
					YOUXIXIU.MAIN.Tips.Show(json.errmsg);
				}
				else
				{
					YOUXIXIU.MAIN.Tips.Show(json.errmsg);
					$(".light_box_fullbg").hide();//蒙层
					$(".album_add_content_div").remove();

					if(!opt.album_id)
					{
						location.href="/user/my_mblog.php";
					}
					
					}
					
					
				}
			
			});		
		

		})
		
	}
	

	function bind_album_add_content_online()
	{

		//绑定提示内容默认值
		YOUXIXIU.MAIN.Tips.text_default_val(".album_add_content_online_pic"); 
		YOUXIXIU.MAIN.Tips.text_default_val(".album_add_content_online_content"); 
		

		//绑定默认选中的类型（如果是加入专辑）
		if(opt.add_mblog_default_topic_id)
		{
			var default_topic = $(".album_add_content_div").find(".sort").find("li[value="+opt.add_mblog_default_topic_id+"]").find("a").html();
			$(".album_add_content_div").find(".sort").find(".select_name").html(default_topic);
			$(".album_add_content_div").find(".sort").find("#add_mblog_topic_id").val(opt.add_mblog_default_topic_id);
		}
		
		

		$(".album_add_content_online_button").unbind("click");
		$(".album_add_content_online_button").bind("click",function(e){
			content = $(".album_add_content_online_content").val();
			pic = $(".album_add_content_online_pic").val();
			topic_id = $(".album_add_content_div").find("#add_mblog_topic_id").val();
			$.getJSON("/interface/album/json_add_pic_url_to_album.php?t=jsonp&album_id="+opt.album_id+"&pic="+pic+"&content="+content+"&topic_id="+topic_id+"&&callback=?", function(json){
			if(typeof(json)!='undefined')
			{
				if(json.error ==1)
				{
					YOUXIXIU.MAIN.Tips.Show(json.errmsg);
				}
				else
				{
					YOUXIXIU.MAIN.Tips.Show(json.errmsg);
					$(".light_box_fullbg").hide();//蒙层
					$(".album_add_content_div").remove();
					if(!opt.album_id)
					{
						location.href="/user/my_mblog.php";
					}
					
					}
					
					
				}
			
			});		
		

		})
		
	}


	function bind_click_submit()
	{
		$(".poup_add_mblog_submit").die("click");
		$(".poup_add_mblog_submit").live("click",function(e){
			

			pid = $("#poup_pid").val();
			pic = $("#poup_pic").val();
			p_width =opt.p_width;
			p_height = opt.p_height;


			content = encodeURIComponent($("#poup_content").val());
			
			if(!pid)
			{
				YOUXIXIU.MAIN.Tips.Show("图片不能为空！");
				return false;
			}
			

			$.getJSON("/interface/mblog/json_add_mblog.php?t=jsonp&album_id="+opt.album_id+"&pid="+pid+"&p_height="+p_height+"&p_width="+p_width+"&pic="+pic+"&content="+content+"&&callback=?", function(json){
			if(typeof(json)!='undefined')
			{
				if(json.error ==1)
				{
					YOUXIXIU.MAIN.Tips.Show(json.errmsg);
				}
				else
				{
					YOUXIXIU.MAIN.Tips.Show(json.errmsg);
					$(".mask").hide();//蒙层
					$("#poup_add_mblog_div").remove();
					if(opt.reload == 0)
					{
						location.href="/mblog/detail.php?id="+json.data.id;
					}
					else
					{
						location.reload();
					}
					
				}

				

				
				
			}
		
		});	
	
		});
	}


	function bind_click_pic()
	{
		$(".choice_pic_button").die("click");
		$(".choice_pic_button").live("click",function(e){
			
			
			$("#poup_add_mblog_pic").toggle();
				options={
				callback:'/js/uploader_callback.html',
				container:'.pic_input_file_div',
				script:'/interface/common/json_mblog_pic_upload.php',
				size:"300|300",
				thumb:1,
				onComplete:function(res){
					$("#poup_add_mblog_pic").hide();
					$("#poup_add_mblog_pic_show").find("#add_mblog_pic_review").attr("src",res.pic2);
					$("#poup_add_mblog_pic_name").html(res.pic_name);
					$("#poup_add_mblog_pic_show").show();
					
					$("#poup_pic").val(res.pic3);
					$("#poup_pid").val(res.pid );

					opt.p_width = res.p_width ;
					opt.p_height = res.p_height ;
					


				},
					errorId:'#errormsg'
				};
				YOUXIXIU.ImgUploader.init(options);
		
		});

		$("#pic_del_toggle").die("click");
		$("#pic_del_toggle").live("click",function(e){
			$("#poup_add_mblog_pic").show();
			$("#poup_add_mblog_pic_show").hide();
		});
		$("#pic_add_mblog_close").die("click");
		$("#pic_add_mblog_close").live("click",function(e){
			$("#poup_add_mblog_pic").hide();
		});
	
	}

	

	function bind_key()
	{
		$(window).keydown(function(event){
			 switch(event.keyCode) {
				case 27 : close_poup();;
		 
				}
	
		});
	
	}

	
	return {
			init: function(options)
			{
				opt=chkDefault(options);
				show_select();
				bindclose();
				bind_key();

			},
			show_select:function(){
				return show_select();
			}
		};

})(jQuery);